import { Application } from 'egg';
import { DataTypes, Model } from 'sequelize';

export type InviteStatus = 'pending' | 'accepted' | 'rejected' | 'canceled';

export interface ExclusiveRestaurantInviteAttributes {
  id?: number;
  restaurant_id: number;
  inviter_id: number;
  invitee_id: number;
  status: InviteStatus;
  remark?: string;
  created_at?: Date;
  updated_at?: Date;
}

export interface ExclusiveRestaurantInviteInstance extends Model<ExclusiveRestaurantInviteAttributes>, ExclusiveRestaurantInviteAttributes {}

export default (app: Application) => {
  const { INTEGER, ENUM, DATE, TEXT } = DataTypes;

  const ExclusiveRestaurantInvite = app.model.define<ExclusiveRestaurantInviteInstance>('ExclusiveRestaurantInvite', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    restaurant_id: { type: INTEGER, allowNull: false, comment: '专属餐厅ID' },
    inviter_id: { type: INTEGER, allowNull: false, comment: '邀请发起人用户ID' },
    invitee_id: { type: INTEGER, allowNull: false, comment: '被邀请用户ID' },
    status: { type: ENUM('pending', 'accepted', 'rejected', 'canceled'), allowNull: false, defaultValue: 'pending', comment: '邀请状态' },
    remark: { type: TEXT, allowNull: true, comment: '备注' },
    created_at: { type: DATE, allowNull: false, defaultValue: DataTypes.NOW },
    updated_at: { type: DATE, allowNull: false, defaultValue: DataTypes.NOW },
  }, {
    tableName: 'exclusive_restaurant_invites',
    comment: '专属餐厅邀请表',
  });

  (ExclusiveRestaurantInvite as any).associate = () => {
    app.model.ExclusiveRestaurantInvite.belongsTo(app.model.ExclusiveRestaurant, { foreignKey: 'restaurant_id', as: 'restaurant' });
    app.model.ExclusiveRestaurantInvite.belongsTo(app.model.User, { foreignKey: 'inviter_id', as: 'inviter' });
    app.model.ExclusiveRestaurantInvite.belongsTo(app.model.User, { foreignKey: 'invitee_id', as: 'invitee' });
  };

  return ExclusiveRestaurantInvite;
};